/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author new
 */
public class DBHandler {

    public int changeDB(String FileName) throws IOException{
        String temp = "DBTemp";
        FileHandler fh = new FileHandler();
        BufferedReader br = fh.openReadFile(FileName);
        BufferedWriter bw = fh.openWriteFile(temp);
        String tempStr = br.readLine();
        while(tempStr != null){
            if(tempStr.contains("DB")){
                bw.append(toByteLine(tempStr));
                bw.newLine();
                tempStr = br.readLine();
                continue;
            }
            bw.append(tempStr);
            bw.newLine();
            tempStr = br.readLine();
        }
        fh.closeReadFile(br);
        fh.closeWriteFile(bw);
        System.gc();
        fh.replaceFile(temp, FileName);
        return 0;
    }

    public String toByteLine(String in) throws FileNotFoundException, IOException{
        Pattern pattern;
        Matcher matcher;
        pattern = Pattern.compile("\\b\\w+\\b",Pattern.CASE_INSENSITIVE);

        String temp = new String(in);
        String result = new String("");
        result = result + temp.substring(0, temp.indexOf("DB") + 2) + " ";
        temp = temp.substring(temp.indexOf("DB") + 2);
        matcher = pattern.matcher(temp);

        while(matcher.find()){
            String tempStr = matcher.group();
            if(tempStr.endsWith("H")){
                for(int i = 0; i < tempStr.length()/2; i++){
                    result = result + toByte(tempStr.substring(i * 2, i * 2 + 2) + "H");
                }
                continue;
            }
            if(tempStr.endsWith("B")){
                for(int i = 0; i < tempStr.length()/8; i++){
                    result = result + toByte(tempStr.substring(i * 8, i * 8 + 8) + "B");
                }
                continue;
            }
            if(!NumberHandler.isDecimal(tempStr)){
                System.out.println(tempStr + "not a decimal number");
                System.exit(0);
            }
            long value = Long.valueOf(tempStr, 10);

            while(value > 65536){
                result = result + toByte("" + (value % 65536));
                value = value / 65536;
            }
            result = result + toByte("" + (value % 65536));
        }

        return result;
    }

    private String toByte(String in) throws FileNotFoundException, IOException{
        String result = new String(in);
        NumberHandler nh = new NumberHandler();
        result = nh.toHex(result);
        return result;
    }

    public int countLength(String in){
        if(in.contains("DB")){
            String temp = in.substring(in.indexOf("DB") + 3);
            return temp.length()/2;
        } else {
            System.out.println("Not have DB.");
            System.exit(0);
            return 0;
        }
    }
}
